package com.fastloan.mgt.invest.controller.comm;

import com.fastloan.mgt.invest.model.InvestRegion;
import com.fastloan.mgt.invest.service.InvestRegionService;
import com.fastloan.safe.core.util.ManagerKit;
import com.kawins.base.baseabs.BaseController;
import com.kawins.result.ResultTo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import tk.mybatis.mapper.entity.Example;

import javax.servlet.http.HttpServletRequest;

/**
 * 区域管理
 *
 * @author rankai
 *         createTime 2017-08-2017/8/10 10:20
 */
@RestController
@RequestMapping("/api/invest/invest")
public class InvestRegionController extends BaseController<InvestRegion> {

    @Autowired
    private InvestRegionService investRegionService;

    /**
     * 区域分页列表
     *
     * @return
     */
    @RequestMapping("/pageList")
    public ResultTo pageList(HttpServletRequest request) {
        Example example = new Example(InvestRegion.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("topOrgId", ManagerKit.getTopOrgId())
                .andEqualTo("state", 1);
        String name = request.getParameter("name");
        if (StringUtils.isNotBlank(name)) {
            criteria.andLike("name", name);
        }
        return dataPage(example);
    }

    /**
     * 获取全部区域
     *
     * @return
     */
    @GetMapping("/all")
    public ResultTo selectAll() {
        Example example = new Example(InvestRegion.class);
        example.createCriteria().andEqualTo("topOrgId", ManagerKit.getTopOrgId())
                .andNotEqualTo("state", 0);
        return new ResultTo().setData(investRegionService.selectByExample(example));
    }

    /**
     * 根据ID获取区域
     *
     * @param regionId 区域Id
     * @return
     */
    @GetMapping("/{regionId}")
    public ResultTo select(@PathVariable("regionId") Integer regionId) {
        InvestRegion investRegion = investRegionService.selectByPrimaryKey(regionId);
        if (investRegion.getState() == 0) {
            return new ResultTo(600, "该项已被删除");
        }
        return new ResultTo().setData(investRegion);
    }

    /**
     * 增加区域
     *
     * @return
     */
    @PostMapping
    public ResultTo insert(InvestRegion investRegion) {
        investRegion.setTopOrgId(ManagerKit.getTopOrgId().toString());
        investRegionService.insertSelective(investRegion);
        return new ResultTo();
    }

    /**
     * 更新区域
     *
     * @return
     */
    @PutMapping
    public ResultTo update(InvestRegion investRegion) {
        investRegion.setTopOrgId(null);
        investRegionService.updateByPrimaryKeySelective(investRegion);
        return new ResultTo();
    }

    /**
     * 判断区域名是否存在
     *
     * @param name 区域名
     * @return
     */
    @GetMapping(params = "name")
    public ResultTo haveName(@RequestParam("name") String name) {
        InvestRegion investRegion = new InvestRegion();
        investRegion.setState(1);
        investRegion.setName(name);
        investRegion.setTopOrgId(ManagerKit.getTopOrgId().toString());
        if (investRegionService.selectCount(investRegion) > 0) {
            return new ResultTo().setData(false);
        }
        return new ResultTo().setData(true);
    }
}
